package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community.entity.ZyRoom;
import com.blb.community.entity.ZyUnit;
import com.blb.community.entity.vo.UnitVO;
import com.blb.community.mapper.ZyUnitMapper;
import com.blb.community.service.IZyUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 单元  服务实现类
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Service
public class ZyUnitServiceImpl extends ServiceImpl<ZyUnitMapper, ZyUnit> implements IZyUnitService {

    @Autowired
    private ZyUnitMapper zyUnitMapper;

    @Override
    public IPage<UnitVO> getListPage( UnitVO unitVO,IPage<UnitVO> page) {
        return zyUnitMapper.getListPage(unitVO,page);
    }

    @Override
    public String saveUnit(UnitVO unitVO) {
        ZyUnit byName = zyUnitMapper.getByName(unitVO);
        if (byName == null){
            zyUnitMapper.insert(unitVO);
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    public String deleteUnit(long unitId) {
        List<ZyRoom> roomByUnitId = zyUnitMapper.getRoomByUnitId(unitId);
        if (roomByUnitId.size() == 0){
            zyUnitMapper.deleteById(unitId);
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String deleteUnitAll(List<Long> unitIds) {
        for (Long id:unitIds){
            List<ZyRoom> roomByUnitId = zyUnitMapper.getRoomByUnitId(id);
            if (!roomByUnitId.isEmpty()){
                return "删除失败";
            }
        }

        zyUnitMapper.deleteBatchIds(unitIds);
        return "删除成功";
    }

//    @Override
//    public String updateUnit(UnitVO unitVO) {
//        ZyUnit byName = zyUnitMapper.getByName(unitVO);
//        if (byName == null){
//            zyUnitMapper.updateById(unitVO);
//            return "修改成功";
//        }
//        return "修改失败";
//    }

    /**
     * 根据buildingName查询对应单元
     * @param buildingName
     * @return
     */
    @Override
    public List<ZyUnit> selectUnit(String buildingName) {
        return zyUnitMapper.selectUnit(buildingName);
    }
}
